package generator;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import misc.Vector3i;

public class Dungeon {
    public static final int roomSpread = 2;
    protected Map<Vector3i, DungeonCell> cells = new HashMap<Vector3i, DungeonCell>();
    protected List<DungeonRoom> rooms = new ArrayList<DungeonRoom>();
    protected List<DungeonTunnel> tunnels = new ArrayList<DungeonTunnel>();

    public void addCell(DungeonCell cell) {
	if(cells.containsKey(cell.getLocation()))
	    System.out.println("WARNING - dungeon cell already exists at location "+cell.getLocation());
	cells.put(cell.getLocation(), cell);
    }
    
    public void addConstruct(DungeonConstruct construct) {
        for(DungeonCell cell : construct.getCells()) {
            addCell(cell);
        }
        if(construct instanceof DungeonRoom)
            rooms.add((DungeonRoom)construct);
        else if(construct instanceof DungeonTunnel)
            tunnels.add((DungeonTunnel)construct);
    }
    
    public boolean cellOccupied(DungeonCell cell) {
        return cells.containsKey(cell.getLocation());
    }
    
    public boolean locationOccupied(Vector3i location) {
        return cells.containsKey(location);
    }
    
    public DungeonCell getCellAtLocation(Vector3i location) {
        return cells.get(location);
    }
    
    public Collection<DungeonConstruct> getOverlappingConstructs(DungeonConstruct construct) {
        List<DungeonConstruct> constructsOverlapping = new ArrayList<DungeonConstruct>();
        for(DungeonRoom currentRoom : rooms) {
            if(constructsOverlapping(construct, currentRoom)) {
                constructsOverlapping.add(currentRoom);
            }
        }
	for(DungeonTunnel currentTunnel : tunnels) {
            if(constructsOverlapping(construct, currentTunnel)) {
                constructsOverlapping.add(currentTunnel);
            }
        }
        return constructsOverlapping;
    }
    
    protected boolean constructsOverlapping(DungeonConstruct construct1, DungeonConstruct construct2) {
        for(DungeonCell cell : construct1.getCells()) {
            if(construct2.locationOccupied(cell.getLocation())) {
                return true;
            }
        }
        return false;
    }

    public Collection<DungeonCell> getCells() {
        return cells.values();
    }
    
    public List<DungeonRoom> getRooms() {
        return rooms;
    }
    
    public List<DungeonTunnel> getTunnels() {
        return tunnels;
    }

    public void removeCell(DungeonCell cell) {
	cells.remove(cell.getLocation());
    }
    
    public void removeConstruct(DungeonConstruct construct) {
	if(construct instanceof DungeonRoom)
	    rooms.remove((DungeonRoom)construct);
	else if(construct instanceof DungeonTunnel)
	    tunnels.remove((DungeonTunnel)construct);
        for(DungeonCell cell : construct.getCells()) {
            removeCell(cell);
        }
    }
}
